﻿#include <iostream>
using namespace std;

class Stack
{
public:
	Stack(int n = 4)
	{
		_a = (int*)malloc(sizeof(int) * n);
		if (_a == nullptr)
		{
			perror("malloc申请空间失败"); 
			return;
		}
		_top = 0;
		_capcaity = n;
	}
	
	~Stack()
	{
		cout << "~Stack" << endl;
		free(_a);
		_top = 0;
		_capcaity = 0;
	}

	void push(int x)
	{
		if (_top == _capcaity)
		{
			int newcapacity = _capcaity * 2;
			int* tmp = (int*)realloc(_a, newcapacity * sizeof(int));

			if (tmp == nullptr)
			{
				perror("fail to realloc");
				return;
			}
			_a = tmp;
			_capcaity = newcapacity;
		}
		_a[_top++] = x;
	}

	//自定义拷贝函数
	Stack(const Stack& st)
	{
		_a= (int*)malloc(sizeof(int) * st._capcaity);
		if (nullptr == _a) 
		{ 
			perror("malloc申请空间失败!!!"); 
			return; 
		}
		memcpy(_a, st._a, sizeof(int) * st._top);

		_top = st._top;
		_capcaity = st._capcaity;
	}


private:
	int* _a;
	int _top;
	int _capcaity;
};



 //两个Stack实现队列 
 class MyQueue 
 {public: 
 private: 
 Stack pushst; 
 Stack popst; 
 };


int main()
{
	Stack st1;
	st1.push(1);
	st1.push(2);

	Stack st2 = st1;
	
	// MyQueue⾃动⽣成的拷⻉构造，会⾃动调⽤Stack拷⻉构造完成pushst/popst 
	// 的拷⻉，只要Stack拷⻉构造⾃⼰实现了深拷⻉，他就没问题
	MyQueue mq1;
	MyQueue mq2 = mq1;

	return 0;
}